<?php
/**
 * 《跟单信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Info.class.php 1037 2010-03-26 11:47:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/CustomerUtil.class.php');

class Info extends CustomerUtil {
	public $tbl = Constant::tbl_customer_info;

	var $info_dir = 'customer';

	public $file = '1';

	public $orderby = array(
		'-' => '请选择',
		'customer' => '客户',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'customer/info/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$info_s = $request['info_s'];

		$my = $this->leader_my('emp');

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($info_s) && $info_s <> '-') {
			$where .= " AND info = " . $info_s;
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['customer_t'] = Name::customer($row['customer']);

				$row['file'] = $this->_file_exist($row['id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->assign('emp', $_SESSION['id']);
		$controller->assign('empname', $_SESSION['name']);

		$controller->display($request, 'customer/info/list');
		return true;
	}

	/**
	 * 添加
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		require_once(dirname(__FILE__) . '/../contact/Customer.class.php');
		$customer = new Customer();
		$controller->assign_by_ref('customer', $customer->_view($id));

		$controller->display($request, 'customer/info/add');
		return true;
	}

	/**
	 * 添加
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$title = addslashes($request['title']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);

		$fmt = "INSERT INTO %s(customer, title, content, emp, createtime) VALUES('%s', '%s', '%s', '%s', now())";
		$sql = sprintf($fmt, $this->tbl, $id, $title, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);

		$ret = array('id'=> $db->insert_id());

		echo json_encode($ret);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		require_once(dirname(__FILE__) . '/../contact/Customer.class.php');
		$customer = new Customer();
		$controller->assign_by_ref('customer', $customer->_view($row['customer']));

		$controller->assign_by_ref('file', $this->_file($row));

		$controller->display($request, 'customer/info/edit');
		return true;
	}

	function aa($controller, $input) {
		require_once 'Mail/mimeDecode.php';

		$params['include_bodies'] = true;
		$params['decode_bodies'] = true;
		$params['decode_headers'] = true;
		$params['input'] = $input;
		// do the decode
		$decoder = new Mail_mimeDecode($input);
		$structure = $decoder->decode($params);

		$structure->headers['from'] = htmlspecialchars($structure->headers['from']);
		$structure->headers['to'] = htmlspecialchars($structure->headers['to']);
		$structure->headers['cc'] = htmlspecialchars($structure->headers['cc']);

		//		while (list($k, $v) = each($structure->headers)) {
		//			debug($k . "=" . $v);
		//		}

		$charset = '';
		// save the attachment as a file.
		foreach ($structure->parts as $part) {
			//			while (list($k, $v) = each($part->headers)) {
			//				debug($k . "|=|" . $v);
			//			}

			if (empty($charset)) {
				$charset = $part->ctype_parameters['charset'];
			}

			if (!empty($charset) && $charset <> 'utf-8') {
				$part->body = iconv($charset, 'utf-8', $part->body);
			}

			// only save if an attachment
			//			if ($part->ctype_primary == 'text') {
			//				debug($part->body);
			//			}
		}
		//		debug('charset = ' . $charset);

		$structure->headers['from'] = iconv($charset, 'utf-8', $structure->headers['from']);
		$structure->headers['to'] = iconv($charset, 'utf-8', $structure->headers['to']);
		$structure->headers['cc'] = iconv($charset, 'utf-8', $structure->headers['cc']);
		$structure->headers['subject'] = iconv($charset, 'utf-8', $structure->headers['subject']);

		$controller->assign_by_ref('mime', $structure);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$title = addslashes($request['title']);
		$content = addslashes($request['content']);

		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET title = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $title, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		require_once(dirname(__FILE__) . '/../contact/Customer.class.php');
		$customer = new Customer();
		$controller->assign_by_ref('customer', $customer->_view($row['customer']));

		$controller->assign_by_ref('file', $this->_file($row));

		$controller->display($request, 'customer/info/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}

		$row = $this->_view_t($row);
		
		return $row;
	}
}
?>